package cn.tedu.person.controller;
import cn.tedu.person.pojo.dto.PersonInsertDTO;
import cn.tedu.person.pojo.dto.PersonUpdateDTO;
import cn.tedu.person.pojo.entity.Person;
import cn.tedu.person.pojo.vo.PersonIndexVO;
import cn.tedu.person.service.IPersonService;
import cn.tedu.person.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/person")
@Api(tags = "01.业主信息")
@Valid
public class PersonController {
    @Value("${imagesPath}")
    private String dirPath;

    @Autowired
    private IPersonService personService;

    //增加业主
    @PostMapping("/add-new")
    @ApiOperation("添加业主信息")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid PersonInsertDTO personInsertDTO) {
        System.out.println("DTO=" + personInsertDTO);
        personService.addNew(personInsertDTO);
        return JsonResult.ok();
    }

    //展示所有的业主
    @GetMapping("/list")
    @ApiOperation("业主信息列表")
    @ApiOperationSupport(order = 200)
    public JsonResult list() {
        List<PersonIndexVO> list=personService.list();
        return JsonResult.ok(list);
    }

    //删除
    @GetMapping("/{id:[0-9]+}/delete")
    @ApiOperation("删除业主信息")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "业主ID", required = true, dataType = "long")
    })
    public JsonResult delete(@Range(min = 1, message = "删除相册失败，ID值无效！")
                                 @PathVariable Long id) {
        System.out.println(id);
        personService.delete(id);
        return JsonResult.ok();
    }

    //按照id查找数据
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询相册详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1, message = "获取相册详情失败，ID值无效！")
                                    @PathVariable Long id) {
        PersonIndexVO queryResult = personService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    //修改
    // http://localhost:9080/person/9527/update
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改相册详情")
    @ApiOperationSupport(order = 500)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "业主ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@Range(min = 1, message = "修改相册详情失败，ID值无效！")
                                     @PathVariable Long id, @Valid PersonUpdateDTO personUpdateDTO) {
        personService.updateInfoById(id, personUpdateDTO);
        return JsonResult.ok();
    }

    //搜索框内通过关键字搜索内容
    // http://localhost:9080/person/selectByWd
    @GetMapping("/selectByWd")
    @ApiOperation("通过名字字段关键字搜索业主信息")
    @ApiOperationSupport(order = 600)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "wd", value = "关键字", required = true, dataType = "string")
    })
    public JsonResult selectByWd(String wd) {
        System.out.println("开始处理搜索功能");
        System.out.println(wd);
            List<PersonIndexVO> selectByWd=personService.selectByWd(wd);
        return JsonResult.ok(selectByWd);

    }

}
